package sel.me.spring.boot245.web.config.security.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import sel.me.spring.boot245.web.exception.UnifyStrErrorCode;
import sel.me.spring.boot245.web.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shenenlu
 * @date 2021-7-28 15:09
 */
@Slf4j
@Component
public class UserLoginFailureHandler implements AuthenticationFailureHandler {
    /**
     * 登录失败返回结果
     * @Author youcong
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception){
        // 这些对于操作的处理类可以根据不同异常进行不同处理
        if (exception instanceof UsernameNotFoundException){
            log.info("【登录失败】"+exception.getMessage());
            ResultUtil.responseJson(response, ResultUtil.resultError(UnifyStrErrorCode.SERVICE_ERROR,"用户名不存在"));
        }

        if (exception instanceof LockedException){
            log.info("【登录失败】"+exception.getMessage());
            ResultUtil.responseJson(response,ResultUtil.resultError(UnifyStrErrorCode.SERVICE_ERROR,"用户被冻结"));
        }
        if (exception instanceof BadCredentialsException){
            log.info("【登录失败】"+exception.getMessage());
            ResultUtil.responseJson(response,ResultUtil.resultError(UnifyStrErrorCode.SERVICE_ERROR,"密码错误"));
        }
        ResultUtil.responseJson(response, ResultUtil.resultError(UnifyStrErrorCode.SERVICE_ERROR,"登录失败"));
    }
}